package com.atyjh.reggie.controller;

import com.atyjh.reggie.dto.Result;
import com.atyjh.reggie.dto.UserDTO;
import com.atyjh.reggie.entity.AddressBook;
import com.atyjh.reggie.service.IAddressBookService;
import com.atyjh.reggie.utils.UserHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.util.List;

import static com.atyjh.reggie.utils.RedisConstants.LOGIN_USER_KEY;

/**
 * @author yjh
 * @since jdk1.8.0_311 2022.06.22
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Resource
    private IAddressBookService addressBookService;

    @GetMapping("/list")
    public Result<List<AddressBook>> list(HttpSession session) {
        UserDTO userDTO = (UserDTO) session.getAttribute(LOGIN_USER_KEY);
        LambdaQueryWrapper<AddressBook> alqw = new LambdaQueryWrapper<>();
        alqw.eq(AddressBook::getUserId, userDTO.getId());
        List<AddressBook> list = addressBookService.list(alqw);
        return Result.success(list);
    }

    @PostMapping
    public Result<String> save(@RequestBody AddressBook addressBook, HttpSession session) {
        UserDTO userDTO = (UserDTO) session.getAttribute(LOGIN_USER_KEY);
        addressBook.setUserId(userDTO.getId());
        addressBookService.save(addressBook);
        return Result.success("保存成功！");
    }

    /**
     * 设置默认收货地址
     *
     * @param addressBook 新的默认地址（只包含id字段）
     * @return 是否设置成功
     */
    @PutMapping("/default")
    @Transactional
    public Result<String> setDefaultAddress(@RequestBody AddressBook addressBook) {

        LambdaUpdateWrapper<AddressBook> addresslqw = new LambdaUpdateWrapper<>();
        addresslqw.eq(AddressBook::getUserId, UserHolder.getUser().getId());
        addresslqw.set(AddressBook::getIsDefault, 0);
//        1. 将当前用户所有地址默认值全部设置为0，即全部都不是默认地址
        addressBookService.update(addresslqw);

//        2. 将当前点击的地址设置为默认地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return Result.success("更新成功");
    }


    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress() {
        LambdaQueryWrapper<AddressBook> ablqw = new LambdaQueryWrapper<>();
        ablqw.eq(AddressBook::getIsDefault, 1)
             .eq(AddressBook::getUserId, UserHolder.getUser().getId());
        return Result.success(addressBookService.getOne(ablqw));
    }

    @GetMapping("/{id}")
    public Result<AddressBook> getAddress(@PathVariable("id") String id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return Result.success("保存成功！");
    }

}
